<?php
class ControllerCommonColumnLeft extends Controller {
	public function index() {
		$this->load->model('design/layout');
		$this->load->model('catalog/category');
		$this->load->model('catalog/product');
		$this->load->model('catalog/information');
		$this->load->model("tool/image");

		if (isset($this->request->get['route'])) {
			$route = (string)$this->request->get['route'];
		} else {
			$route = 'common/home';
		}
		$layout_id = 0;
		if ($route == 'product/category' && isset($this->request->get['path'])) {
			$path = explode('_', (string)$this->request->get['path']);
			$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
		}

		if ($route == 'product/product' && isset($this->request->get['product_id'])) {
			$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
		}
		if ($route == 'information/information' && isset($this->request->get['information_id'])) {
			$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
		}

		if (!$layout_id) {
			$layout_id = $this->model_design_layout->getLayout($route);
		}
		if (!$layout_id) {
			$layout_id = $this->config->get('config_layout_id');
		}

		$data['modules'] = array();

		$modules = $this->model_design_layout->getLayoutModules($layout_id, 'column_left');
		foreach ($modules as $module) {
			$part = explode('.', $module['code']);
			if (isset($part[0])) {
				$code = $part[0];
			}
			if ($code && $this->config->get($code . '_status')) { 
				$setting = $this->config->get($code . '_module');
				if (isset($part[1]) && isset($setting[$part[1]])) {
					$data_controller = $data['modules'][] = $this->load->controller('module/' . $code, $setting[$part[1]]);
				} else {
					
					$data['modules'][] = $this->load->controller('module/' . $code);
				}			
			}
		}
		$data["selling_product"] = "";// san pham duoc mua nhieu nhat
		$data["gift_product"] = ""; // san pham duoc khuyen mai
		if($route == "product/category"){
			$this->load->language("product/product");
			$data["title_selling_product"] = $this->language->get("text_selling_product");
			$selling_product = $this->model_catalog_product->getProductSelling() ;	
			if(!empty($selling_product)){
				foreach($selling_product as $item){
					if ($item['image']) {
						$image = $this->model_tool_image->resize($item['image'],160,205);
					} else {
						$image = false;
					}					
					if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
						$price = $this->currency->format($this->tax->calculate($item['price'], $item['tax_class_id'], $this->config->get('config_tax')));
					} else {
						$price = false;
					}
					$data["selling_product"][]  =  array(
						'product_id' => $item['product_id'],
						'thumb'   	 => $image,
						'name'    	 => $item['name'],
						'price'   	 => $price,
						'href'    	 => $this->url->link('product/product', 'product_id=' . $item['product_id']),
					);
				}	
			}	
			
			$data["title_gift_product"] = $this->language->get("text_gift_product");
			$gift_product = $this->model_catalog_product->getGiftProduct();	
			if($gift_product){
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($gift_product['price'], $gift_product['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}
				$data["gift_product"] = array(
					"product_id" 	=>$gift_product["product_id"],
					"price"		 	=> $price,
					'name'    	 => $item['name'],
					'href'    	 	=> $this->url->link('product/product', 'product_id=' . $gift_product['product_id']),
					'content_gift' => $gift_product["content_gift"],					
				);	
			}
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/column_left.tpl')) {
			return $this->load->view($this->config->get('config_template') . '/template/common/column_left.tpl', $data);
		} else {
			return $this->load->view('default/template/common/column_left.tpl', $data);
		}
	}
}